viby = {
loadScript: function(filename, onload) {
	var fileref = document.createElement('script');
	fileref.setAttribute("type","text/javascript");
	fileref.setAttribute("src", filename);
	document.getElementsByTagName("head")[0].appendChild(fileref);
	fileref.onload = onload;
},

addBubble: function(bubble) {
	var textbox = (bubble.direction === 'left') ? this.textleft : this.textright;
	textbox.append(bubble.container);
},

addFeedItem: function(type, data) {
	bubble = new Bubble('right');

	switch (type) {
	case 'user-text':
		bubble.setText(data);
		break;
	case 'viby-text':
		bubble.setText('Viby: ' + data);
		break;
	case 'weather':
		break;
	}

	this.addBubble(bubble);
	//this.feedbox.animate({ scrollTop: this.feedbox.prop("scrollHeight") }, 500);
},

userInput: function(text) {
	$.ajax({
		type: 'POST',
		url: "../server/viby.php",
		context: this,
		data: {user: text, session: document.cookie.substring(10)},
		success: function(response){
			this.addFeedItem('viby-text', response);
		},
		error: function(){
			this.addFeedItem('viby-text', "I couldn't connect to the server");
		}
	});

	this.addFeedItem('user-text', text);
}
};

$(document).ready(function() {
	var input = $('#input-box');
	//viby.feedbox = $('#feed-box');
	viby.textleft = $('#text-area-left');
	viby.textright = $('#text-area-right');

	viby.loadScript('bubble.js', function() {
		var weather = new Bubble();
		weather.setText('Weather');
		weather.addChoice('Go');
		weather.addChoice('bla');
		viby.addBubble(weather);
		var doctor = new Bubble(true);
		doctor.setText('Doctor');
		doctor.addChoice('Go');
		viby.addBubble(doctor);
	});

	viby.loadScript('eyecursor.js', function() {
		new vibyEye(100,70,60,'viby-area');
		new vibyEye(130,65,60,'viby-area');
	});

	input.focus();
	input.keypress(function(e){
		if(e.which == 13 && input.val() !== ''){
			viby.userInput(input.val());
			input.val('');
		}
	});
});
